package com.chanyachting.emall.action.companyproduct;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.chanyachting.emall.domain.Companyproduct;
import com.chanyachting.emall.form.CompanyProductForm;
import com.chanyachting.emall.service.ProductimageBO;
import com.chanyachting.emall.util.DateUtil;
import com.chanyachting.emall.util.MyFile;
import com.chanyachting.emall.util.RequestUtil;

public class UpdateNomalProductAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		
		RequestUtil ru=new RequestUtil(request);
		
		int id=Integer.parseInt(ru.getParameter("id","0"));
		
		if(request.getMethod().equals("POST")){
			
			CompanyProductForm cpform=(CompanyProductForm) form;
			
			try {
				
				if(cpform.validate(mapping, request).isEmpty()){
					
					Companyproduct cp=new Companyproduct(id);
					
					BeanUtils.copyProperties(cp, cpform);
					
					String imgUrl=MyFile.saveFileForm(cpform.getFile(), request);
					
					if(imgUrl!=null && !imgUrl.equals("")) cp.setImgUrl(imgUrl);  //如果商品图片修改，设置新的图片路径

					cp.setUpdateDate(DateUtil.getDate());  //设置更新时间
					
					if(cp.update()){
						
						//产品信息修改成功，修改图片信息
						
						
						ru.setSuccessMsg();
					}else{
						
						ru.setFailMsg();
					}
					
					
				}

			
			} catch (Exception e) {
			
				e.printStackTrace();
			}
			
		}
		

		ru.setAttribute("companyproduct", new Companyproduct(id));
		
		return mapping.findForward("nomalupdate");
	}
	
	
	

}
